package com.huangsm.book.designmodel.parallel.masterworker;

import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Master代码
 * @author huang
 * @date 2019/2/19
 */
public class Master {

    /**
     * 任务队列
     */
    protected Queue<Object>workQueue=new ConcurrentLinkedQueue<>();
    /**
     * Worker进程队列
     */
    protected Map<String,Thread>threadMap=new HashMap<String,Thread>();
    /**
     * 子任务处理结果集
     */
    protected Map<String,Object>resultMap=new ConcurrentHashMap<>();

    /**
     * 是否所有子任务都结束了
     * @return
     */
    public boolean isComplete(){
        for (Map.Entry<String, Thread> entry : threadMap.entrySet()) {
            if (entry.getValue().getState()!=Thread.State.TERMINATED){
                return false;
            }
        }
        return true;
    }

    /**
     * Master的构造，需要一个Worker进程逻辑，和需要的Worker进程数
     * @param countWorker
     */
    public Master(Worker worker,int countWorker){
        worker.setWorkQueue(workQueue);
        worker.setResultMap(resultMap);
        for (int i = 0; i < countWorker; i++) {
            threadMap.put(Integer.toString(i),new Thread(worker,Integer.toString(i)));
        }
    }

    /**
     * 提交一个任务
     * @param obj
     */
    public void submit(Object obj){
        workQueue.add(obj);
    }

    /**
     * 返回子任务结果集
     * @return
     */
    public Map<String,Object>getResultMap(){
        return resultMap;
    }

    /**
     * 开始执行所有的Worker进程，进行处理
     */
    public void execute(){
        for (Map.Entry<String, Thread> entry : threadMap.entrySet()) {
            entry.getValue().getState();
        }
    }
}
